import { tileLayer } from 'leaflet'
import { tiledMapLayer, imageMapLayer } from '@supermap/iclient-leaflet'
import { MarkerClusterGroup } from 'leaflet.markercluster'
import AbstractLayer from './AbstractLayer'
import Toolkit from '../Toolkit'
/**
 * @classdesc 创建各种Layer
 * @author Niyang
 */
export default class Layer extends AbstractLayer {
    /**
     * @description 创建Layer
     * @param {String} type
     * @param {Object} options
     * @returns Layer
     */
    build (type, options) {
        let layer = null
        switch (type) {
        case 'tile':
            layer = tileLayer(options.url, options)
            break
        case 'tile-image':
            layer = tiledMapLayer(options.url, options)
            break
        case 'image':
            layer = imageMapLayer(options.url, options)
            break
        case 'cluster':
            layer = new MarkerClusterGroup(options)
            break
        default:
            break
        }
        return layer
    }

    /**
     * @description 获取Layer
     * @param {Object} options
     * @returns Layer实体
     */
    getLayer (options) {
        const { type } = options
        if (Toolkit.isUndef(type)) {
            console.error(new Error('layer type is required'))
            return null
        }
        return this.build(type.toLowerCase(), options)
    }
}
